/**
 * clsSche_BatchRoundRobinLQ is a schedulable class which schedules BatchRoundRobinLQ 
 * for doing the lead assignment in a round robin fashion.
 */
global class clsSche_BatchRoundRobinLQ implements Schedulable{
	
	//Cron expression to the time at which the class is schduled for the first time.
	public static string CRON_EXP = '0 50 * * * ? *';
	
	/**Excutes the batch class and reschedules the class to run after 10 mins. It
	 * also aborts the existing job, so that it doesnt increase the number of jobs
	 * scheduled at an instant to be greater than 25
	 */ 
	global void execute(SchedulableContext SC) {
       BatchRoundRobinLQ batch = new BatchRoundRobinLQ();
       Database.executeBatch(batch,200);
       
       datetime nextScheduleTime = system.now().addMinutes(10);
     	string minute = string.valueof(nextScheduleTime.minute());
     	string second = string.valueof(nextScheduleTime.second ());
     	string cronvalue = second+' '+minute+' * * * ?' ;
   		string jobName = 'Batch Round Robin' + nextScheduleTime.format('hh:mm');

   		clsSche_BatchRoundRobinLQ p = new clsSche_BatchRoundRobinLQ();
   		system.schedule(jobName, cronvalue , p);

   // this section of code will abort the current schedule job
   		system.abortJob(sc.getTriggerId());
   }
   
   //This is the method run using developer console to schedule the class for the first time.
   public static void start(){
   		system.schedule('Batch Round Robin', CRON_EXP,new clsSche_BatchRoundRobinLQ() );  
   		
   }  
}